<?php

/**
 * 发票辅助方法
 * User: xiaolin
 * Date: 16/8/31
 * Time: 上午10:51
 */
class invoice_helper
{
    /**
     * 根据发票的ID获取订单的数量
     * @author xiaolin
     * @date   2016-08-31
     * @return int
     */
    public static function get_order_total_by_invoiceId($invoice_id) {
        return _model('invoice_relation')->getTotal(array(
            'invoice_id' => $invoice_id,
            'status' => 1 //未被驳回或者删除
        ));
    }

    /**
     * 获取用户可开票金额(上个月剩余额度到今天额度的总和)
     * @author xiaolin
     * @date   2016-08-31
     * @return decimal
     */
    public static function get_user_invoice_amount($user_id, $object_type = null) {
        $array_type = array();
        if ($object_type) {
            $array_type['object_type'] = $object_type;
        }

        $filter = array(
            'user_id' => $user_id,
            'month' => array(date('Y-m', strtotime('-1 month')), date('Y-m'))
        );
        $filter = array_merge($filter, $array_type);

        $amount = _model('user_invoice_amount')->getFields('amount', $filter);
        return $amount ? format_money(array_sum($amount)) : 0.00;
    }

    /**
     * 增加或减少可开发票额度
     * @author xiaolin
     * @date   2016-08-31
     * @param int $user_id 用户ID
     * @param decimal $price 变动金额
     * @param int $object_type 1:合伙人给门店开票
     * @param int $operate 操作(1:增加,2:减少)
     * @param int $is_ts 是否支持事务(0:不支持,1:支持)
     * @return array
     */
    public static function change_user_invoice_amount($user_id, $price, $object_type = 1, $operate = 0, $is_ts = 0) {
        $price = format_money($price);
        $array_type = array();
        $array_type['object_type'] = $object_type;

        //增加
        if ($operate == 1) {
            $filter = array(
                'user_id' => $user_id,
                'month' => date('Y-m'),
            );
            $filter = array_merge($filter, $array_type);
            $data = _model('user_invoice_amount')->field('id', 'amount')->read($filter);

            //不存在先添加一条记录
            if (!$data) {
                $create_data = array(
                    'user_id' => $user_id,
                    'month' => date('Y-m'),
                    'amount' => $price,
                    'add_time' => date('Y-m-d H:i:s'),
                    'update_time' => date('Y-m-d H:i:s')
                );
                $create_data = array_merge($create_data, $array_type);
                _model_ts('user_invoice_amount', $is_ts)->create($create_data);

                return array('flag' => 0, 'data' => array());
            }

            //存在则修改记录,增加当月的额度
            $data['amount'] = format_money($data['amount']) + $price;
            _model_ts('user_invoice_amount', $is_ts)->update($data['id'], array(
                'amount' => $data['amount'],
                'update_time' => date('Y-m-d H:i:s')
            ));

            return array('flag' => 0, 'data' => array());
        } elseif ($operate == 2) { //减少
            $filter = array(
                'user_id' => $user_id,
                'month' => array(date('Y-m', strtotime('-1 month')), date('Y-m')),
            );
            $filter = array_merge($filter, $array_type);

            $data = _model('user_invoice_amount')->field('id', 'amount', 'month')->order('id')->getList($filter);
            $data = format_money($data, 'amount');

            //没有记录说明这两个月没进货,额度为0,不能操作减少
            if (!$data) {
                return array('flag' => 1);
            }

            if ($price > $data[0]['amount']) {
                //如果开票金额大于前一个月的额度,那么先将前一个月额度清零,最后再减少当月的额度
                $log = array();
                if ($data[0]['amount'] != 0.00) {
                    _model_ts('user_invoice_amount', $is_ts)->update($data[0]['id'], array(
                        'amount' => 0.00,
                        'update_time' => date('Y-m-d H:i:s')
                    ));
                    $log[$data[0]['month']] = format_money($data[0]['amount']);
                }

                _model_ts('user_invoice_amount', $is_ts)->update($data[1]['id'], array(
                    'amount' => format_money($data[0]['amount'] + $data[1]['amount'] - $price),
                    'update_time' => date('Y-m-d H:i:s')
                ));

                $log[$data[1]['month']] = format_money($price - $data[0]['amount']);
                $log = json_encode($log, JSON_BIGINT_AS_STRING | JSON_UNESCAPED_UNICODE);

                return array('flag' => 0, 'data' => $log);
            } else {
                //如果开票金额小于前一个月的额度,那么直接用前一个月的额度减去开票金额即可
                _model_ts('user_invoice_amount', $is_ts)->update($data[0]['id'], array(
                    'amount' => format_money($data[0]['amount'] - $price),
                    'update_time' => date('Y-m-d H:i:s')
                ));

                $log = json_encode(array($data[0]['month'] => format_money($price)), JSON_BIGINT_AS_STRING |
                                                                                     JSON_UNESCAPED_UNICODE);
                return array('flag' => 0, 'data' => $log);
            }
        }
    }

    /**
     * 根据发票ID获取用户信息
     * @author xiaolin
     * @date   2016-09-14
     * @return array
     */
    public static function get_user_by_invoice_id($invoice_id) {
        $invoice = _uri('invoice', $invoice_id);
        return _uri('user', $invoice['user_id']);
    }

    /**
     * 根据发票ID获取发票信息
     * @author xiaolin
     * @date   2016-10-28
     * @return array
     */
    public static function get_invoice_by_id($id) {
        return _uri('invoice', $id);
    }

    /**
     * 根据订单ID获取发票信息
     * @author xiaolin
     * @date   2016-10-28
     * @return array
     */
    public static function get_invoice_by_order_id($order_id) {
        $invoice_relation = _uri('invoice_relation', array(
            'order_id' => $order_id,
            'status' => 1
        ));
        return _uri('invoice', $invoice_relation['invoice_id']);
    }

    /**
     * 判断是否可开票
     * @author xiaolin
     * @date   2016-09-18
     * @return json
     */
    public static function is_enable_make_invoice($user_id) {
        $is_invoice = user_helper::get_is_invoice($user_id);
        return $is_invoice ? true : false;
    }
}